<!--
- 这个文件用于普通微信登录（公众号）
- 得到openId
-->
<template>
	<div class="login-wrap">
		<!--<div class="c-blue f14px">微信公众号登录中....</div>-->
		<!--<div>code: {{code}}</div>-->
		<!--<div>得到的openId:{{openId}}</div>-->
		<!--<div>-->
		<!--	<el-button type="success" @click="backToCouponDetail">←返回领券</el-button>-->
		<!--	<el-button type="danger" @click="loginRequest">手动登录</el-button>-->
		<!--</div>-->
	</div>
</template>

<script>
import { mapMutations, mapState } from 'vuex'
import { apiWxGetOpenId, apiWxWorkerLogin } from "@/axios/api";
import { getQueryString, isEmpty, isNotEmpty } from "@/utils/common";
import mixins from "@/mixins/mixins";
import mixinsWx from "@/mixins/mixinsWx";
export default {
	name: "loginWx",
	components: {},
	mixins:[mixins,mixinsWx],
	data() {
		return {
			openId: "",
			code:"",//跳转回来时的参数cdoe
		}
	},
	computed:{},
	created(){
	},
	mounted(){
		this.loginRequest()
	},
	methods:{

		...mapMutations([
			// 'userWork/SET_USERINFO', // -> this['some/nested/module/foo']()
			// 'userWork/SET_TOKEN', // -> this['some/nested/module/foo']()
		]),

		//登录请求
		loginRequest(){

			//获取code参数，由于企业跳转回来的url把参数塞在path后面，使用这个方法
			let _code = this.$route.query.code//先使用路由方式获取参数
			console.log('code',_code)
			if(isEmpty(_code)){
				_code = getQueryString('code')
				console.log('code2',_code)
			}
			this.code = _code

			//成功拿到code再执行登录
			if( isNotEmpty(_code) ) {
				/*Swal.fire({
					// position:'top',
					html: '微信公众号登录请求中,请用稍后...',
					// type: "success",
					showConfirmButton: false,
					backdrop:true,//遮罩
					allowOutsideClick: false,//允许外部点击关闭
					onBeforeOpen: () => { Swal.showLoading() },
					onAfterClose: () => {}
				});*/
				//请求服务器,获取openid
				apiWxGetOpenId({
					code:_code
				}).then((res)=>{
					// this.notify('服务器返回:'+JSON.stringify(res),'success')
					if(res.resultFlag == true){
						this.openId = res.data //用于显示
						localStorage.setItem("openId",res.data)
						/*Swal.fire({
							position:'center',
							html: '登录成功，点击返回',
							type: "success",
							showConfirmButton: true,
							backdrop:false,//遮罩
							allowOutsideClick: false,//允许外部点击关闭
							// onBeforeOpen: () => { Swal.showLoading() },
							onAfterClose: () => {
								this.backToCouponDetail()
							}
						});*/

						setTimeout(() => {
							this.backToCouponDetail()
						}, 200)

					}else{
						this.$message.error(res.resultMsg)
					}
				})
			}else{
				this.notify('code为空，无法继续进行处理','danger')
			}

		},


		//返回领券
		backToCouponDetail(){
			this.$router.push('/couponGet')
		}

	}
}
</script>

<style lang="less" scoped>

.login-wrap{
	word-break:break-all;
	* {word-break:break-all;}
}

</style>
